//
//  chaxunjichuTableViewController.m
//  PASystem
//
//  Created by michael on 16/9/27.
//  Copyright © 2016年 czd. All rights reserved.
//

#import "chaxunjichuTableViewController.h"
#import "ProvinceDetailController.h"

@interface chaxunjichuTableViewController ()
@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,assign) NSInteger currentPage;
@end

@implementation chaxunjichuTableViewController

static NSString *const ID = @"Cell";
-(NSMutableArray *)dataArray{

    if (_dataArray == nil) {
        
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;

}
- (void)viewDidLoad {
    [super viewDidLoad];
        self.view.backgroundColor = [UIColor whiteColor];
    self.title = _string;

   __weak typeof(self) weakSelf = self;
    [self.tableView addHeaderWithCallback:^{
    weakSelf.currentPage = 1;

        [weakSelf loadData]; //加载网络数据
    }];
    [self.tableView headerBeginRefreshing];
    
    [self.tableView addFooterWithCallback:^{
                     [weakSelf loadMore];
       
       
    }];
}
-(void)loadData{
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSString * pwdId = [[NSUserDefaults standardUserDefaults]objectForKey:@"pwdId"];NSString * userHumber = [[NSUserDefaults standardUserDefaults]objectForKey:@"userHumber"];
    if ([_name isEqualToString:@"全部单位"]) {
    _name = @"0";
}else if([_name isEqualToString:@"副会长单位"]){
    _name = @"1";


}else if([_name isEqualToString:@"常务理事单位"]){
    _name = @"2";


}else if([_name isEqualToString:@"理事单位"]){
    _name = @"3";


}else if([_name isEqualToString:@"普通会员"]){
    _name = @"4";


}else {
    _name = @"5";


}
   if (!_timebeforestring){
   _timebeforestring = @"";
   
   
   }else{
   
   _timebeforestring = [NSString stringWithFormat:@",begintime:%@",_timebeforestring];
   }
   
   
    if (!_timeafterstring){
   _timeafterstring = @"";
   
   
   }else{
   
   _timeafterstring = [NSString stringWithFormat:@",endtime:%@",_timeafterstring];
   }

  if ([_namestring isEqualToString:@""]){
   _namestring = @"";
   
   
   }else{
   
   _namestring = [NSString stringWithFormat:@",name:%@",_namestring];
   }
    
 NSString *json =[NSString stringWithFormat: @"{'action':getJCLibrariesHygl,'service':MemberService, 'userName':'%@','password':'%@', 'vaueJson':'{userid :%@,pageNum:%ld%@%@%@}'}",userId,pwdId,userHumber,self.currentPage,_timebeforestring,_timeafterstring,_namestring];
     NSArray * array = @[@{@"arg0":json}];
    [HttpTools requestWithURL:BaseURL arrays:array method:Basemethod success:^(id JSON) {
        
        NSArray *arr = JSON[@"msg"];
        
        for (NSDictionary *dic in arr) {
            
            NSDictionary *dict = dic[@"attrs"];
            
            leftModel *leftM = [leftModel mj_objectWithKeyValues:dict];
            
            [self.dataArray addObject:leftM];
            
        }
        
        [self.tableView headerEndRefreshing];
        
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
     [self.tableView headerEndRefreshing];
        
    }];
 
}
- (void)loadMore{
    self.currentPage ++;

    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSString * pwdId = [[NSUserDefaults standardUserDefaults]objectForKey:@"pwdId"];NSString * userHumber = [[NSUserDefaults standardUserDefaults]objectForKey:@"userHumber"];
    if ([_name isEqualToString:@"全部单位"]) {
    _name = @"0";
}else if([_name isEqualToString:@"副会长单位"]){
    _name = @"1";


}else if([_name isEqualToString:@"常务理事单位"]){
    _name = @"2";


}else if([_name isEqualToString:@"理事单位"]){
    _name = @"3";


}else if([_name isEqualToString:@"普通会员"]){
    _name = @"4";


}else {
    _name = @"5";


}
   if (!_timebeforestring){
   _timebeforestring = @"";
   
   
   }else{
   
   _timebeforestring = [NSString stringWithFormat:@",begintime:%@",_timebeforestring];
   }
   
   
    if (!_timeafterstring){
   _timeafterstring = @"";
   
   
   }else{
   
   _timeafterstring = [NSString stringWithFormat:@",endtime:%@",_timeafterstring];
   }

  if ([_namestring isEqualToString:@""]){
   _namestring = @"";
   
   
   }else{
   
   _namestring = [NSString stringWithFormat:@",name:%@",_namestring];
   }
    
 NSString *json =[NSString stringWithFormat: @"{'action':getJCLibrariesHygl,'service':MemberService, 'userName':'%@','password':'%@', 'vaueJson':'{userid :%@,pageNum:%ld%@%@%@}'}",userId,pwdId,userHumber,self.currentPage,_timebeforestring,_timeafterstring,_namestring];
     NSArray * array = @[@{@"arg0":json}];
    [HttpTools requestWithURL:BaseURL arrays:array method:Basemethod success:^(id JSON) {
        
        NSArray *arr = JSON[@"msg"];
        
        for (NSDictionary *dic in arr) {
            
            NSDictionary *dict = dic[@"attrs"];
            
            leftModel *leftM = [leftModel mj_objectWithKeyValues:dict];
            
            [self.dataArray addObject:leftM];
            
        }
        
        if (self.dataArray.count == 0) {
            
            [self.view makeToast:@"没有最新数据哦"];
        }
        
        
        [self.tableView footerEndRefreshing];
        
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
     [self.tableView headerEndRefreshing];
        
    }];
 
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    leftModel *model = self.dataArray[indexPath.row];
    
    leftTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (cell == nil) {
        
        cell = [leftTableViewCell WithLeftTableViewCell];
        
    }
    
    cell.model = model;
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 60;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    leftModel *model = self.dataArray[indexPath.row];
    
    ProvinceDetailController *proVC = [[ProvinceDetailController alloc] init];
    
    proVC.contId = model.ID;
    
    [self.navigationController pushViewController:proVC animated:YES];
    
}

@end
